package com.qf.controller;


import com.qf.entity.Health;
import com.qf.entity.Organ;
import com.qf.service.HealthService;
import com.qf.service.Impl.HealthServiceImpl;
import com.qf.service.Impl.OrganServiceImpl;
import com.qf.service.OrganService;
import com.qf.util.Constants;
import com.qf.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@WebServlet("/health")
public class HealthController extends BaseController {

    public String showAllHealth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageIndex = request.getParameter("pageIndex");
        if (pageIndex == null) {
            pageIndex = "1";
        }
        Page page = new Page(Integer.valueOf(pageIndex));
        HealthService healthService = new HealthServiceImpl();
        List<Health> healthList = healthService.showAllHealth(page);

        request.setAttribute("page", page);
        request.setAttribute("healthList", healthList);
        return Constants.FORWARD + "/list.jsp";
    }

    //修改先查询当前用户的
    public String updateHealth(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));

        HealthService healthService = new HealthServiceImpl();
        Health health = healthService.showByHealth(id);
        request.setAttribute("health", health);

        OrganService organService = new OrganServiceImpl();
        List<Organ> organList = organService.selectAllOrgan();
        request.setAttribute("organList", organList);

        return Constants.FORWARD + "/edit.jsp";
    }

    //新增 修改
    public String modify(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String projectname = request.getParameter("projectname");
        Integer status = Integer.valueOf(request.getParameter("status"));
        Double money = Double.parseDouble(request.getParameter("money"));
        Integer organId = Integer.valueOf(request.getParameter("organId"));
        Date date = null;
        String deadline = request.getParameter("deadline");
        try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            date = simpleDateFormat.parse(deadline);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Health health = new Health(0, projectname, status, date, money, organId);

        if (id == "") {
            HealthService healthService = new HealthServiceImpl();
            healthService.add(health);
        } else {
            health.setId(Integer.valueOf(id));
            HealthService healthService = new HealthServiceImpl();
            healthService.update(health);
        }
        return Constants.FORWARD + "health?method=showAllHealth";
    }

    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer id = Integer.valueOf(request.getParameter("id"));
        HealthService healthService = new HealthServiceImpl();
        Integer i = healthService.delete(id);
        return i.toString();
    }

}
